﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CAOS.Entity;
using CAOS.Context;

namespace CAOS.UnityOfWork
{
    public class UnidadeDeTrabalho
    {
        private CAOSContext context = new CAOSContext();

        #region Repositórios

        private SistemaRepository sistemaRepository;

        public SistemaRepository SistemaRepository
        {
            get
            {
                if (this.sistemaRepository == null)
                {
                    this.sistemaRepository = new SistemaRepository(context);
                }
                return sistemaRepository;
            }
        }

        private UsuarioRepository usuarioRepository;

        public UsuarioRepository UsuarioRepository
        {
            get
            {
                if (this.usuarioRepository == null)
                {
                    this.usuarioRepository = new UsuarioRepository(context);
                }
                return usuarioRepository;
            }
        }

        private PapelRepository papelRepository;

        public PapelRepository PapelRepository
        {
            get
            {
                if (this.papelRepository == null)
                {
                    this.papelRepository = new PapelRepository(context);
                }
                return papelRepository;
            }
        }

        private RecursoRepository recursoRepository;

        public RecursoRepository RecursoRepository
        {
            get
            {
                if (this.recursoRepository == null)
                {
                    this.recursoRepository = new RecursoRepository(context);
                }
                return recursoRepository;
            }
        }
        
        private GenericRepository<TrilhaAuditoria> logRepository;

        public GenericRepository<TrilhaAuditoria> LogRepository
        {
            get
            {
                if (this.logRepository == null)
                {
                    this.logRepository = new GenericRepository<TrilhaAuditoria>(context);
                }

                return logRepository;
            }
        }

        #endregion

        #region Métodos

        public void Save()
        {
            try
            {
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                TratadorException.TratarException(ex);
            }
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
